require 'map_navigator'
require 'side_bar'
require 'map_editor/map_panel'
require 'map_editor/map_container'
require 'map_editor/palet_container'
require 'map_editor/map_controler/map_drawer'
require 'event_editor/event_controler'
require 'map_editor/map_controler/collision_setter'
require 'event_editor/event_list_panel'
require "map_editor/palet_controler/palet_picker"
require "map_editor/palet_controler/palet_base_setter"
require "map_editor/palet_controler/palet_collision_setter"

class MainContainer < Gtk::HBox
  attr_reader :side_bar, :palet_container, :map_navigator
  attr_reader :map_model, :event_model

  include MapEditor
  include EventEditor
  def initialize
    super
    table = Gtk::Table.new(2, 1)

    @side_bar = Gtk::Notebook.new
    @side_bar.set_width_request(296)

    vpaned = Gtk::VPaned.new
    
    @side_bar.show_tabs = false

    @palet_container = PaletContainer.new
    @side_bar.append_page(@palet_container)

    @event_list_panel = EventListPanel.new
    @side_bar.append_page(@event_list_panel)
    
    vpaned.pack1(@side_bar, true, true)
    @map_navigator = MapNavigator.new(self, 200)
    vpaned.pack2(@map_navigator, true, false)
    table.attach(vpaned, 0, 1, 0, 1, 0, Gtk::EXPAND | Gtk::FILL, 0)
    
    @map_container = MapContainer.new(self)
    table.attach(@map_container, 1, 2, 0, 1, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL)
    self.add(table)
    @side_bar.next_page
    
    @map_navigator.load
  end

  def clear_map_container
    @map_model = nil
    @event_model = nil
    @map_container.clear
    @palet_container.clear
    GC.start
  end
  
  def load_map(map_model, event_model)
    @map_model = map_model
    @event_model = event_model
    self.set_palet_panels(map_model)
    @palet_container.active = true
    @map_container.map_panel = MapPanel.new(map_model, @map_container.zoom_index, self.generate_map_controlers, @map_container.panel_mode, @map_container.current_layer_no, @map_container.show_layers)
    GC.start
  end
  
  def set_palet_panels(map_model)
    t = NormalPaletPanel.new(map_model, self.generate_palet_controlers, :palet_picker)

    t.load_chipset(map_model.base_chipset)
    @palet_container.palet_notebook.basic_palet_panel = t

    t = AutoPaletPanel.new(map_model, self.generate_palet_controlers, :palet_picker)
    t.load_chipsets(map_model.auto_chipsets)
    @palet_container.palet_notebook.auto_palet_panel = t
    
    t = AnimePaletPanel.new(map_model, self.generate_palet_controlers, :palet_picker)
    t.load_chipset(map_model.anime_chipset)
    @palet_container.palet_notebook.anime_palet_panel = t
  end
  
  def generate_map_controlers
    return {
            :map_drawer => MapControler::MapDrawer.new(@palet_container.palet_notebook), 
            :event_controler => EventControler.new(event_model, @event_list_panel), 
            :collision_setter => MapControler::CollisionSetter.new(map_model)
           }
  end
  
  def generate_palet_controlers
    return {
            :palet_picker => PaletControler::PaletPicker.new, 
            :palet_base_setter => PaletControler::PaletBaseSetter.new, 
            :palet_collision_setter => PaletControler::PaletCollisionSetter.new
           }
  end
  
  def palet_notebook
    return @palet_container.palet_notebook
  end
  
  def map_panel
    return @map_container.map_panel
  end
  
  def on_resize(width, height)
    @map_container.on_resize(width - @side_bar.width_request, height)
  end
end